package com.csdj.exception;

import com.csdj.utils.ResultInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.BindException;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常
 */
@ControllerAdvice
public class GlobalExceptionHandler {


 /*   @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultInfo exceptionHandler(Exception e){
        ResultInfo resultInfo=new ResultInfo();
        resultInfo.setCode(300);
        resultInfo.setMsg("操作失败!");
        if(e instanceof ParamsException){
            ParamsException pe= (ParamsException) e;
            resultInfo.setMsg(pe.getMsg());
            resultInfo.setCode(pe.getCode());
        }else if(e instanceof BindException){
            BindException be = (BindException) e;

            resultInfo.setResult(be.getMessage());
        }
        return resultInfo;
    }*/

  @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map<String,Object> exceptionHandler(Exception e){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",300);
        map.put("msg","操作失败"+e.getMessage());
        if(e instanceof ParamsException){
            ParamsException pe= (ParamsException) e;
            map.put("code",pe.getCode());
            map.put("msg",pe.getMsg());
        }else if(e instanceof BindException){
            BindException be= (BindException) e;
        }

        return map;
    }

   /* @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(Exception ex, Model model){
        Map<String,Object> map=new HashMap<>();
        if(ex instanceof ParamsException){
            map.put("code",300);
            map.put("msg","操作失败"+ex.getMessage());
        }else if(ex instanceof BusinessException){
            map.put("code",300);
            map.put("msg","操作失败!"+ex.getMessage());
        }else{
            map.put("code",300);
            map.put("msg","操作失败!"+ex.getMessage());
        }

        model.addAttribute("message",map);
        return "error";
    }
*/


}
